package net.fornwall.eclipsecoder.preferences;

import net.fornwall.eclipsecoder.util.Utilities;

import org.eclipse.jface.preference.IPreferenceNode;
import org.eclipse.jface.preference.IPreferencePage;
import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.jface.preference.PreferenceManager;
import org.eclipse.jface.preference.PreferenceNode;
import org.eclipse.ui.plugin.AbstractUIPlugin;

public class EclipseCoderPlugin extends AbstractUIPlugin {

	private static EclipseCoderPlugin instance;

	public static final String PLUGIN_ID = "net.fornwall.eclipsecoder";

	public static EclipseCoderPlugin getDefault() {
		return instance;
	}

	public static boolean tcAutoLogon() {
		return instance.getPreferenceStore().getBoolean(
				PreferenceConstants.TC_AUTOLOGON);
	}

	public static String tcPassword() {
		return instance.getPreferenceStore().getString(
				PreferenceConstants.TC_PASSWORD);
	}

	public static String tcUserName() {
		return instance.getPreferenceStore().getString(
				PreferenceConstants.TC_USERNAME);
	}

	public static boolean isTcAccountSpecified() {
		String user = tcUserName();
		String pass = tcPassword();
		return user != null && !(user.length() == 0) && pass != null
				&& !(pass.length() == 0);
	}

	public EclipseCoderPlugin() {
		super();
		EclipseCoderPlugin.instance = this;
	}

	/**
	 * Show a message to the user specifying that a TopCoder member account is
	 * needed. Follow this by opening the preferences page allowing him to enter
	 * information about this account.
	 */
	public static void demandTcAccountSpecified() {
		Utilities.runInDisplayThread(new Runnable() {
			public void run() {
				Utilities
						.showMessageDialog("TopCoder account needed",
								"You need to specify your TopCoder account credentials.");
				IPreferencePage page = new PreferencesPage();
				PreferenceManager mgr = new PreferenceManager();
				IPreferenceNode node = new PreferenceNode(PreferencesPage.class
						.getCanonicalName(), page);
				mgr.addToRoot(node);
				PreferenceDialog dialog = new PreferenceDialog(Utilities
						.getWindowShell(), mgr);
				dialog.create();
				dialog.setMessage(page.getTitle());
				dialog.open();
			}

		});
	}
}
